#include <string>
#include <iostream>

#include "gtest/gtest.h"

#include "data_store.h"
#include "double_item.h"
#include "section_item.h"

TEST(Data_store, deep_get)
{
  Data_store store;

  Item *section = store.get_item();
  Item *new_section_item = new Section_item("my section");
  section->add_item(new_section_item);

  Item *new_section_item2 = new Section_item("test 3");
  new_section_item->add_item(new_section_item2);

  Item *double_item = new Double_item("my doubles");
  double_item->add_double(4.5);
  double_item->add_double(4);
  double_item->add_double(45);
  new_section_item2->add_item(double_item);

  Item *i = section->get_item_deep("my section/test 3/my doubles");
  ASSERT_TRUE(i != NULL);
  ASSERT_EQ("my doubles", i->get_name());
  ASSERT_EQ(45, i->get_double(2));
}

TEST(Data_store,Tokenize)
{
  Data_store store;

  string s("hello my friend how \"are you\"");

  string delimiter = " ";

  vector<string> st = store.tokenize(s, delimiter);

  ASSERT_EQ( "hello", st[0]);
  ASSERT_EQ( "my", st[1]);
  ASSERT_EQ( "friend", st[2]);
  ASSERT_EQ( "how", st[3]);
  ASSERT_EQ( "are you", st[4]);
}

TEST(Data_store,save_multiple_doubles)
{
  Data_store store;

  store.set_name("test");

  Item *section = store.get_item();

  Item *double_item = new Double_item("my doubles");
  double_item->add_double(4.5);
  double_item->add_double(4);
  double_item->add_double(45);
  section->add_item(double_item);

  Item *section_item = new Section_item("my section");
  section->add_item(section_item);

  double_item = new Double_item("my other doubles");
  double_item->add_double(4.4);
  double_item->add_double(6);
  double_item->add_double(455);

  section_item->add_item(double_item);

  store.save();
}

TEST(Data_store,save_doubles_and_ints)
{
  Data_store store;

  store.set_name("test");

  Item *section = store.get_item();

  Item *double_item = new Double_item("my doubles");
  double_item->add_double(4.5);
  section->add_item(double_item);

  Item *integer_item = new Integer_item("my ints");
  integer_item->add_integer(98);
  section->add_item(integer_item);

  Item *section_item = new Section_item("my section");
  section->add_item(section_item);

  double_item = new Double_item("my other doubles");
  double_item->add_double(4.1);

  section_item->add_item(double_item);

  store.save();
}

TEST(Data_store,delete_item)
{
  Item *double_item = new Double_item("my doubles");
  double_item->add_double(4.5);
  double_item->add_double(4);
  double_item->add_double(45);

  Item *section_item = new Section_item("my section");
  section_item->add_item(double_item);

  ASSERT_EQ(1, section_item->get_count());

  section_item->delete_item("my doubles");

  ASSERT_EQ(0, section_item->get_count());
}

TEST(Data_store,double_clear)
{
  Item *double_item = new Double_item("my doubles");
  double_item->add_double(4.5);
  double_item->add_double(93.2);

  ASSERT_EQ(4.5, double_item->get_double(0));
  ASSERT_EQ(93.2, double_item->get_double(1));
  ASSERT_EQ(2, double_item->get_count());

  double_item->clear();
  ASSERT_EQ(0, double_item->get_count());

}

TEST(Data_store,load)
{
  Data_store store;

  store.set_name("test");

  store.load();

  // todo: test values
}
